/*
 * @lc app=leetcode.cn id=202 lang=javascript
 *
 * [202] 快乐数
 */

// @lc code=start
/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
    let arr = []
    let flag = ''
    while (true) {
        if (n === 1) {
            flag = true
            break
        }
        arr.push(n)
        n += ''
        let sum = 0
        for (let index = 0; index < n.length; index++) {
            const element = n[index];
            sum += element * element
        }
        n = sum
        for (let index = 0; index < arr.length; index++) {
            const element = arr[index];
            if (element === n) {
                flag = false
            }
        }
        if (flag !== '') {
            break
        }
    }
    return flag
};
// @lc code=end

